﻿using System;
using System.Windows.Forms;
using PowerMonitor.Common;
using PowerMonitor.Properties;

namespace PowerMonitor.Upgrade
{
    public partial class RegForm : Form
    {
        static Computer cmp = null;
        public RegForm()
        {
            InitializeComponent();
            cmp = Computer.Instance();
            CheckRegister();
            txtLocalSerial.Text = cmp.CpuID;
            if (AppMain.isRegisted)
            {
                txtRegID.Text = (txtLocalSerial.Text + "2015").GetHashCode().ToString();
            }
            else
            {
                txtRegID.Text = "";
            }
            txtLocalSerial.Enabled = !AppMain.isRegisted;
            txtRegID.Enabled = !AppMain.isRegisted;
            btnRegiste.Enabled = !AppMain.isRegisted;
            lbRegistered.Visible = AppMain.isRegisted;
        }

        private void btnRegiste_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            string strCorrectSN = "";
            if (txtLocalSerial.Text != "")
            {
                strCorrectSN = (txtLocalSerial.Text + "2015").GetHashCode().ToString();
            }
            else
            {
                errorProvider1.SetError(txtLocalSerial,Resources.txtPleaseInputSN);
            }
            if (txtRegID.Text != strCorrectSN)
            {
                errorProvider1.SetError(txtRegID, Resources.txtErrRegisteCode);
            }
            else
            {
                INIFile file = new INIFile(AppMain.StartPath + Resources.RegisterPath + "regster.ini");
                file.ClearAllSection();
                file.IniWriteValue("程序信息", "序列号", txtLocalSerial.Text + "2015");
                file.IniWriteValue("程序信息", "注册ID", txtRegID.Text);
                AppMain.isRegisted = true;
                MessageBox.Show(Resources.txtRegisteSucceed, Properties.Resources.msgTitleTip, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            txtLocalSerial.Enabled = !AppMain.isRegisted;
            txtRegID.Enabled = !AppMain.isRegisted;
            btnRegiste.Enabled = !AppMain.isRegisted;
            lbRegistered.Visible = AppMain.isRegisted;
        }
        /// <summary>
        /// 检查注册信息
        /// </summary>
        static public void CheckRegister()
        {
            cmp = Computer.Instance();
            INIFile file = new INIFile(AppMain.StartPath + Resources.RegisterPath + "regster.ini");
            file.ClearAllSection();
            string strSerial = (cmp.CpuID + "2015").GetHashCode().ToString();
            file.IniWriteValue("程序信息", "序列号", cmp.CpuID + "2015");
            string strID = file.IniReadValue("程序信息", "注册ID");
            if (strSerial != strID)
            {
                AppMain.isRegisted = false;
            }
            else
            {
                AppMain.isRegisted = true;
            }
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
